package com.ujiuye.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.ujiuye.config.MessageConstant;
import com.ujiuye.config.Result;
import com.ujiuye.pojo.Menu;
import com.ujiuye.service.IMenuService;
import com.ujiuye.vo.MenuVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单 前端控制器
 * </p>
 *
 * @author zs
 * @since 2021-07-05
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private IMenuService menuService;

    @GetMapping("list")
    public Result getMenu(){
        try {
            List<MenuVo> menuVos = menuService.listMenu();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,menuVos);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") int id){
        try {
            Menu menu = menuService.getById(id);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }

    }
    @GetMapping
    public Result getParent(){
        try {
            List<Menu> parent = menuService.getParent();
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,parent);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }

    @PutMapping
    public  Result update(@RequestBody Menu menu){
        try {
            menuService.updateById(menu);
            return new Result(true,MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MENU_FAIL);
        }
    }
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable("id") int id){
        try {
            menuService.removeById(id);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.DELETE_MENU_FAIL);
        }
    }




    


}

